﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SD.Database.Classes;

namespace DataAccess
{
    public class AuthorizeEventArgs : EventArgs
    {
        public bool Authorized { get; set; }

        public AuthorizeEventArgs(bool success)
        {
            Authorized = success;
        }
    }
    public class Player
    {
        public delegate void AuthorizeUserDelegate(AuthorizeEventArgs e);

        public static event AuthorizeUserDelegate AuthorizeUserCompleted;

        static WebClient client = new WebClient();
        public static void BeginAuthorizeUser(string login, string password)
        {
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://localhost/webservice/sd.php?fun=" + Security.Encrypt("Authorize") + "&login=" +
                Security.Encrypt(login) + "&has=" + Security.Encrypt(Security.GetMd5Sum(login + password))), string.Empty);
        }

        static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                AuthorizeUserCompleted.Invoke(new AuthorizeEventArgs(false));
            else
            {
                string t = Security.Decrypt(e.Result);
                if (t == "False")
                    AuthorizeUserCompleted.Invoke(new AuthorizeEventArgs(false));
                else
                    AuthorizeUserCompleted.Invoke(new AuthorizeEventArgs(true));
            }
        }

    }
}
